/** @format */

const toObjectString = Object.prototype.toString;

/* export function isObject(val: any): val is Object {
  return val !== null && typeof val === 'object';
} */

export function isDate(val: any): val is Date {
  return toObjectString.call(val) === '[object Date]';
}

export function isPlainObject(val: any): val is Record<any, any> {
  return toObjectString.call(val) === '[object Object]';
}

export function extend<T, U>(to: T, from: U): T & U {
  for (const key in from) {
    (to as T & U)[key] = from[key] as any;
  }
  return to as T & U;
}

export function deepMerge(...objs: Array<any>): any {
  const result = Object.create(null);
  objs.forEach(obj => {
    if (obj) {
      Object.keys(obj).forEach(key => {
        const val = obj[key];
        if (isPlainObject(val)) {
          // 如果result中已经存在key(即key重复)
          if (isPlainObject(result[key])) {
            result[key] = deepMerge(result[key], val);
          } else {
            result[key] = deepMerge(val);
          }
        } else {
          result[key] = val;
        }
      });
    }
  });
  return result;
}
